/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye.global;

import android.R;
import android.app.Activity;
import android.text.util.Linkify;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 23.05.11
 * Time: 1:05
 */
public class UiHelper {

    public static void setStringArrayToSpinner(Activity activity, int spinnerId, int arrayId) {
        Spinner spinner = (Spinner) activity.findViewById(spinnerId);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(activity, arrayId, R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    @SuppressWarnings("unchecked")
    public static void setSpinnerSelection(Activity activity, int spinnerId, String value) {
        Spinner spinner = (Spinner) activity.findViewById(spinnerId);
        ArrayAdapter<String> adapter = (ArrayAdapter<String>) spinner.getAdapter();
        int selection = adapter.getPosition(value);
        spinner.setSelection(selection);
    }

    public static void addLink(final Activity activity, int spannableId, String pattern, int schemeId, final int linkId) {
        Pattern p = Pattern.compile(pattern);
        TextView textView = (TextView) activity.findViewById(spannableId);
        Linkify.addLinks(textView,
                p,
                activity.getString(schemeId),
                null,
                new Linkify.TransformFilter() {
                    @Override
                    public String transformUrl(Matcher match, String url) {
                        return activity.getString(linkId);
                    }
                }
        );
    }
}
